/**
 * Created by shen'jia.
 */
;(function(){
  'use strict';
  angular.module('controllers.oldOMDisplay.oldSystemPerform',[])
  .controller('oldSystemPerformMainController', oldSystemPerformMainController)//main
  .controller('oldSystemPerformController', oldSystemPerformController)//controller
  .controller('oldSystemStagePerformController', oldSystemStagePerformController)//stageController
  .controller('oldSystemSetSysPerformController', oldSystemSetSysPerformController);//setController
//main
  function oldSystemPerformMainController ($scope, CoreService, oldSystemPerformData, oldSystemPerformURL, oldSystemPerformCRUD){
    var spans= ['/sysPerform','/setPerform'];
    $scope.selectedIndex= CoreService.navgit(spans);             //导航样式
    $scope.$on('$stateChangeSuccess', function(evt, state) {
      $scope.selectedIndex = spans.indexOf(state.url);
    });
    $scope.selector= oldSystemPerformData;                       //下拉列表的数据
    $scope.queryBusiness= oldSystemPerformURL.getSysBusiness;
    $scope.queryBusiness('11');
    $scope.resetForm = oldSystemPerformCRUD.resetForm;           //重置表单

    $scope.changeSys = function(sysCode){
      $scope.queryBusiness(sysCode);
      if(sysCode === '11'){
        $scope.place = '签约';
        oldSystemPerformData.translate.businessCode = '02';
      }else{
        $scope.place = '个人服务开通';
        oldSystemPerformData.translate.businessCode = '12';
      }
    };

    $scope.$on('$destroy', function(){
      oldSystemPerformData.translate.startTime = new Date();
      oldSystemPerformData.translate.endTime = new Date();
    });
    var year=new Date().getFullYear();
    var month = new Date().getMonth();
    var day = new Date().getDate();
  $scope.$watch('selector.translate.timeTypeCode',function(value){
    if(value==='1'){
      $scope.selector.translate.startTime=new Date(year,month,day-5)
    }else{
       $scope.selector.translate.startTime=new Date(year,month-5,day);
    }
  })

  }
//controller
  function oldSystemPerformController($scope, oldSystemPerformData, oldSystemPerformURL, oldSystemPerformOption){
    $scope.shoumun = {flag:1};
    var sysPerformChart = echarts.init(document.getElementById('sysPerfm')); //初始化echarts
    var option = oldSystemPerformOption.sysPerformChart;                   //echarts的option
    /**
     * [searchST 查询方法]
     * @return {[type]} [description]
     */
  
    $scope.searchST = function(){
      if($scope.shoumun.flag === 1){
        return;
      }
      $scope.sysCode = oldSystemPerformData.translate.systemCode;
      $scope.busCode = oldSystemPerformData.translate.businessCode;
      for(var i = 0;i<oldSystemPerformData.system.length;i++){
        if($scope.sysCode === oldSystemPerformData.system[i].systemCode){
          $scope.ssyy = oldSystemPerformData.system[i].systemName;
        }
      }
      for(var i = 0;i<oldSystemPerformData.business.length;i++){
        if($scope.busCode === oldSystemPerformData.business[i].businessCode){
          $scope.bbuu = oldSystemPerformData.business[i].businessName;
        }
      }
      // var businessNN = $scope._getNames($scope.selector.translate.business,'businessName',$scope.selector.business);
      // var systemN = $scope.selector.translate.system.length > 0 ? $scope.selector.translate.system[0].systemName : '全部系统';
      // var businessN = $scope.selector.translate.business.length > 0 ? businessNN : '全部业务';
      if($scope.bbuu === undefined){
        ($scope.ssyy === '统一支付') ? ($scope.bbuu = '签约') : ($scope.bbuu = '个人服务开通');
      }
      var title = $scope.ssyy + $scope.bbuu + '响应时间';
      oldSystemPerformURL.querySysPerfm(sysPerformChart,option,title,true);
    };
  }
//stageController
  function oldSystemStagePerformController($scope, oldSystemPerformURL, oldSystemPerformProcessStageData,$state){
    $scope.title = 'stage';

    $scope.returnSysperform=function(){
      $state.go('sas.oldOMDisplay.sysPerf.sysPerform');
    }
    //var queryParams = JSON.parse(window.localStorage.sysPerfmCondition);
    var throughputChart = echarts.init(document.getElementById('throughput'));   //办理数、吞吐量
    var concurrentChart = echarts.init(document.getElementById('concurrent'));   //并发图
    var phasesResChart = echarts.init(document.getElementById('phasesRes'));     //（总阶段）响应时间
    var drillChart = echarts.init(document.getElementById('drill'));             //阶段响应时间
    var echartsObj = [throughputChart,concurrentChart,phasesResChart,drillChart];//4个图的实例化数组
    var titles = ['业务吞吐量','业务并发数','响应时间','阶段响应时间'];               //4个图的表名
    $scope.isDrill = true;                                                       //下钻控制变量
    //阶段请求
    oldSystemPerformURL.queryPhases({},function(resultData){
      if(resultData.state === '10000'){
        $scope.isQuery = true;
        if(resultData.data.length === 0){
          sysPerfmChart.clear();
          sysPerfmChart.setOption({ series : {data:[]} });
          return;
        }
        oldSystemPerformProcessStageData.process(echartsObj,titles,resultData.data,$scope);
      }else{
        CoreService.dialog('根据查询条件未查到有关数据','提示('+resultData.state+')');
        $scope.isQuery = false;
      }
    });
  }
//setController
  function oldSystemSetSysPerformController($scope,oldSystemPerformData,oldSystemPerformCRUD,oldSystemPerformBox,$state,oldSystemPerformAddData){
    
    $scope.addLi = oldSystemPerformAddData.addDate;

    $scope.shoumun = {flag:1};
    $scope.addP = oldSystemPerformCRUD.addTime;                 //新增
    $scope.modifyP = oldSystemPerformCRUD.modifyTime;           //修改
    // $scope.delP = oldSystemPerformCRUD.deleteTime;               //删除
    
    $scope.changeSys = function(sysCode){
      $scope.queryBusiness(sysCode);
      if(sysCode === '11'){
        $scope.place = '签约';
        oldSystemPerformData.translate.businessCode = '02';
      }else{
        $scope.place = '个人服务开通';
        oldSystemPerformData.translate.businessCode = '11';
      }
    };

    $scope.watchAddForm = function(pro){
      oldSystemPerformData.chaProvince = pro.ob;
      $state.go('sas.oldOMDisplay.sysPerf.watchAdd');
    };          //查看表单

    $scope.watchForm = function(pro){
      oldSystemPerformData.chaProvince = pro.ob;
      $state.go('sas.oldOMDisplay.sysPerf.watch');
    };          //查看表单

    $scope.getIds = oldSystemPerformBox;
    $scope.checkli = [
      {ob :'',checked:false}
    ];

    $scope.searchST = function(){
      if($scope.shoumun.flag === 1){
        return;
      }
      oldSystemPerformData.checckBox = [];
      $scope.checkli = [
        {ob :'',checked:false}
      ];
      $scope.sysCode = oldSystemPerformData.translate.systemCode;
      $scope.busCode = oldSystemPerformData.translate.businessCode;
      for(var i = 0;i<oldSystemPerformData.system.length;i++){
        if($scope.sysCode === oldSystemPerformData.system[i].systemCode){
          $scope.ssyy = oldSystemPerformData.system[i].systemName;
        }
      }
      for(var i = 0;i<oldSystemPerformData.business.length;i++){
        if($scope.busCode === oldSystemPerformData.business[i].businessCode){
          $scope.bbuu = oldSystemPerformData.business[i].businessName;
        }
      }
      if(oldSystemPerformData.translate.province.length >0){
        angular.forEach(oldSystemPerformData.translate.province,function(role,key){
          $scope.checkli[key] = {ob :'',checked:false};
          $scope.checkli[key].ob = role.provinceName;
          $scope.checkli[key].checked = false;
        });
      }else{
        $scope.checkli[0].ob = '广东';
      }

    };
  };


})();